package com.hxzy.service.system.impl;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxzy.dto.system.DictTypeDTO;
import com.hxzy.entity.system.SysDictType;
import com.hxzy.mapper.system.SysDictTypeMapper;
import com.hxzy.service.system.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;


@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper,SysDictType> implements SysDictTypeService{

    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Override
    public IPage<SysDictType> selectAll(DictTypeDTO dictTypeDTO) {
        LambdaQueryWrapper<SysDictType> queryWrapper=new LambdaQueryWrapper<>();

        if (dictTypeDTO.getDictName()!= null){
            queryWrapper.like(SysDictType::getDictName,dictTypeDTO.getDictName());
        }
        if (dictTypeDTO.getStatus()!= null){
            queryWrapper.eq(SysDictType::getStatus,dictTypeDTO.getStatus());
        }
        if (dictTypeDTO.getDictType()!= null){
            queryWrapper.like(SysDictType::getDictType,dictTypeDTO.getDictType());
        }
        //判断日期
        if(Objects.nonNull(dictTypeDTO.getStartTime()) && Objects.nonNull(dictTypeDTO.getEndTime())){
            //日期要多1天
            Date endTime=  DateUtil.offsetDay( dictTypeDTO.getEndTime(), 1) ;
            queryWrapper.between(SysDictType::getCreateTime, dictTypeDTO.getStartTime(),endTime);
        }
        //拼查询分页对象
        IPage<SysDictType> page= PageDTO.of(dictTypeDTO.getPage(), dictTypeDTO.getSize());
        return this.page(page,queryWrapper);
    }

    @Override
    public SysDictType findType(Long id) {
        return this.sysDictTypeMapper.findType(id);
    }
}
